home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 7_4.lha / 7_4 / 7_4a.c next >
Text File  |  1993-08-08  |  2KB  |  65 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / exercise 7.4
  6. / connect two shapes
  7. include <shape.h>
  8.  
  9. / return the square of the
  10. / distance between two points
  11. nline int dist(point a, point b)
  12.  
  13.    int deltax = a.x - b.x;
  14.    int deltay = a.y - b.y;
  15.    return deltax * deltax + deltay * deltay;
  16.  
  17.  
  18. / find the closest contact-points between
  19. / two shapes and draw a line between them
  20. oid connectshapes(const shape *a, const shape *b)
  21.  
  22.    // save each of the 8 points relevant to each shape
  23.    point apts[8], bpts[8];
  24.    point *ptptr = apts;
  25.    const shape *shptr = a;
  26.  
  27.    for (int i = 0; i < 2; i++, shptr = b, ptptr = bpts)
  28. {
  29. *ptptr++ = shptr->north();
  30. *ptptr++ = shptr->neast();
  31. *ptptr++ = shptr->east();
  32. *ptptr++ = shptr->seast();
  33. *ptptr++ = shptr->south();
  34. *ptptr++ = shptr->swest();
  35. *ptptr++ = shptr->west();
  36. *ptptr++ = shptr->nwest();
  37. }
  38.  
  39.    // save one set of points and their distance
  40.    point pta = apts[0], ptb = bpts[0];
  41.    int mindist = dist(pta, ptb);
  42.  
  43.    // find closest point on shape b to pta
  44.    int ndist;
  45.    for (i = 1; i < 8; i++)
  46. if ((ndist = dist(pta, bpts[i])) < mindist)
  47.     {
  48.     ptb = bpts[i];
  49.     mindist = ndist;
  50.     }
  51.  
  52.    // look for a set of points even closer
  53.    for (i = 1; i < 8; i++)
  54. for (int j = 0; j < 8; j++)
  55.     if ((ndist = dist(apts[i], bpts[j])) < mindist)
  56.     {
  57.     pta = apts[i];
  58.     ptb = bpts[j];
  59.     mindist = ndist;
  60.     }
  61.  
  62.    // draw a line between the two points
  63.    put_line(pta, ptb);
  64.  
  65.